﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKElementFinish.ColumnSurface.DataDefinition;
using YJKElementFinish.ColumnSurface.Managers;

namespace YJKElementFinish.MultiWallEdit.Forms
{
	public partial class WallTypeSplitForm : System.Windows.Forms.Form
	{
		public List<WallTypeLayerInfos> GetUserNewTypeByWallType(WallType oldType)
		{
			int index = this.m_UserSelectTypeList.FindIndex((WallTypeLayerInfos item) => item.TypeName.CompareTo(oldType.Name) == 0);
			return this.m_UserNewTypeList[index];
		}

		public WallTypeSplitForm(List<WallType> userSelectTypeList, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.m_Doc = doc;
			this.InitializeWallLayersInfoList(userSelectTypeList);
		}

		private void InitializeWallLayersInfoList(List<WallType> userSelectTypeList)
		{
			foreach (WallType wallType in userSelectTypeList)
			{
				WallTypeLayerInfos wallLayerInfoListByType = WallTypeManager.GetWallLayerInfoListByType(wallType, this.m_Doc);
				this.m_UserSelectTypeList.Add(wallLayerInfoListByType);
				List<WallTypeLayerInfos> list = new List<WallTypeLayerInfos>();
				list.Add(new WallTypeLayerInfos(wallLayerInfoListByType));
				this.m_UserNewTypeList.Add(list);
				this.m_OperationTypeByType.Add(WallTypeSplitForm.OperationType.OT_NotSplit);
			}
		}

		private void WallTypeSplitForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.UpdateUserSelectTreeView();
		}

		private void WallTypeSplitForm_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		private void treeViewUserSelect_AfterSelect(object sender, TreeViewEventArgs e)
		{
			int index = -1;
			if (!this.IsSelectedWallTypeNode(ref index))
			{
				return;
			}
			this.SetCurrentOperationType(this.m_OperationTypeByType[index]);
			this.UpdateUserNewTreeView(false);
		}

		private bool IsSelectedWallTypeNode(ref int selectIndex)
		{
			TreeNode treeNode = this.treeViewUserSelect.SelectedNode;
			if (treeNode.Tag == null)
			{
				if (treeNode.Parent == null)
				{
					return false;
				}
				if (treeNode.Parent.Tag == null)
				{
					return false;
				}
				treeNode = treeNode.Parent;
			}
			string text = treeNode.Tag.ToString();
			if (text.CompareTo("") == 0)
			{
				return false;
			}
			selectIndex = Convert.ToInt32(text);
			return true;
		}

		private void UpdateUserSelectTreeView()
		{
			for (int i = 0; i < this.m_UserSelectTypeList.Count; i++)
			{
				WallTypeLayerInfos wallTypeLayerInfos = this.m_UserSelectTypeList[i];
				this.UpdateViewTypeCompoundStructure(wallTypeLayerInfos, i, true);
			}
			this.treeViewUserSelect.SelectedNode = this.treeViewUserSelect.Nodes[0];
		}

		private void UpdateUserNewTreeView(bool isReset)
		{
			int num = -1;
			if (!this.IsSelectedWallTypeNode(ref num))
			{
				return;
			}
			if (isReset)
			{
				WallTypeSplitForm.OperationType operationType = this.CurrentOperationType();
				if (operationType == WallTypeSplitForm.OperationType.OT_NotSplit)
				{
					this.SetNotSplitData(num);
				}
				else if (operationType == WallTypeSplitForm.OperationType.OT_WholeSurfaceLayer)
				{
					this.SpliteByWholeSurfaceLayer(num);
				}
				else if (operationType == WallTypeSplitForm.OperationType.OT_MultiSurfaceLayer)
				{
					this.SpliteByMultiSurfaceLayer(num);
				}
				else
				{
					this.SpliteByCustom(num);
				}
			}
			this.treeViewUserNew.Nodes.Clear();
			for (int i = 0; i < this.m_UserNewTypeList[num].Count; i++)
			{
				WallTypeLayerInfos wallTypeLayerInfos = this.m_UserNewTypeList[num][i];
				this.UpdateViewTypeCompoundStructure(wallTypeLayerInfos, i, false);
			}
		}

		private WallTypeSplitForm.OperationType CurrentOperationType()
		{
			if (this.rdbNotSplit.Checked)
			{
				return WallTypeSplitForm.OperationType.OT_NotSplit;
			}
			if (this.rdbSplitStyle1.Checked)
			{
				return WallTypeSplitForm.OperationType.OT_WholeSurfaceLayer;
			}
			if (this.rdbSplitStyle2.Checked)
			{
				return WallTypeSplitForm.OperationType.OT_MultiSurfaceLayer;
			}
			return WallTypeSplitForm.OperationType.OT_Custom;
		}

		private void SetCurrentOperationType(WallTypeSplitForm.OperationType operationType)
		{
			if (operationType == WallTypeSplitForm.OperationType.OT_NotSplit)
			{
				this.rdbNotSplit.Checked = true;
			}
			else if (operationType == WallTypeSplitForm.OperationType.OT_WholeSurfaceLayer)
			{
				this.rdbSplitStyle1.Checked = true;
			}
			else
			{
				if (operationType != WallTypeSplitForm.OperationType.OT_MultiSurfaceLayer)
				{
					this.rdbCustom.Checked = true;
					this.btnCombination.Enabled = true;
					this.btnDecomposition.Enabled = true;
					this.treeViewUserNew.CheckBoxes = true;
					this.treeViewUserNew.DrawMode = TreeViewDrawMode.OwnerDrawAll;
					return;
				}
				this.rdbSplitStyle2.Checked = true;
			}
			this.btnCombination.Enabled = false;
			this.btnDecomposition.Enabled = false;
			this.treeViewUserNew.CheckBoxes = false;
			this.treeViewUserNew.DrawMode = TreeViewDrawMode.Normal;
		}

		private void SetNotSplitData(int selectIndex)
		{
			List<WallTypeLayerInfos> list = new List<WallTypeLayerInfos>();
			list.Add(new WallTypeLayerInfos(this.m_UserSelectTypeList[selectIndex]));
			this.m_UserNewTypeList[selectIndex] = list;
		}

		private void SpliteByWholeSurfaceLayer(int selectIndex)
		{
			WallTypeLayerInfos wallTypeLayerInfos = this.m_UserSelectTypeList[selectIndex];
			List<WallTypeLayerInfos> list = new List<WallTypeLayerInfos>();
			List<WallLayerInfos> list2 = new List<WallLayerInfos>();
			int i;
			for (i = 0; i < wallTypeLayerInfos.FirstCoreLayerIndex; i++)
			{
				WallLayerInfos item = wallTypeLayerInfos.LayerList[i];
				list2.Add(item);
			}
			if (list2.Count > 0)
			{
				WallTypeLayerInfos item2 = this.CreateWallTypeLayerInfosByWallLayerInfosList(list2);
				list.Add(item2);
			}
			List<WallLayerInfos> list3 = new List<WallLayerInfos>();
			while (i < wallTypeLayerInfos.LastCoreLayerIndex + 1)
			{
				WallLayerInfos item3 = wallTypeLayerInfos.LayerList[i];
				list3.Add(item3);
				i++;
			}
			WallTypeLayerInfos item4 = this.CreateWallTypeLayerInfosByWallLayerInfosList(list3);
			list.Add(item4);
			List<WallLayerInfos> list4 = new List<WallLayerInfos>();
			while (i < wallTypeLayerInfos.LayerList.Count)
			{
				WallLayerInfos item5 = wallTypeLayerInfos.LayerList[i];
				list4.Add(item5);
				i++;
			}
			if (list4.Count > 0)
			{
				WallTypeLayerInfos item6 = this.CreateWallTypeLayerInfosByWallLayerInfosList(list4);
				list.Add(item6);
			}
			this.m_UserNewTypeList[selectIndex] = list;
		}

		private void SpliteByMultiSurfaceLayer(int selectIndex)
		{
			WallTypeLayerInfos wallTypeLayerInfos = this.m_UserSelectTypeList[selectIndex];
			List<WallTypeLayerInfos> list = new List<WallTypeLayerInfos>();
			int i;
			for (i = 0; i < wallTypeLayerInfos.FirstCoreLayerIndex; i++)
			{
				WallLayerInfos layerInfos = wallTypeLayerInfos.LayerList[i];
				WallTypeLayerInfos item = this.CreateWallTypeLayerInfosByWallLayerInfos(layerInfos);
				list.Add(item);
			}
			List<WallLayerInfos> list2 = new List<WallLayerInfos>();
			while (i < wallTypeLayerInfos.LastCoreLayerIndex + 1)
			{
				WallLayerInfos item2 = wallTypeLayerInfos.LayerList[i];
				list2.Add(item2);
				i++;
			}
			WallTypeLayerInfos item3 = this.CreateWallTypeLayerInfosByWallLayerInfosList(list2);
			list.Add(item3);
			while (i < wallTypeLayerInfos.LayerList.Count)
			{
				WallLayerInfos layerInfos2 = wallTypeLayerInfos.LayerList[i];
				WallTypeLayerInfos item4 = this.CreateWallTypeLayerInfosByWallLayerInfos(layerInfos2);
				list.Add(item4);
				i++;
			}
			this.m_UserNewTypeList[selectIndex] = list;
		}

		private void SpliteByCustom(int selectIndex)
		{
			WallTypeLayerInfos wallTypeLayerInfos = this.m_UserSelectTypeList[selectIndex];
			List<WallTypeLayerInfos> list = new List<WallTypeLayerInfos>();
			foreach (WallLayerInfos layerInfos in wallTypeLayerInfos.LayerList)
			{
				WallTypeLayerInfos item = this.CreateWallTypeLayerInfosByWallLayerInfos(layerInfos);
				list.Add(item);
			}
			this.m_UserNewTypeList[selectIndex] = list;
		}

		private void UpdateViewTypeCompoundStructure(WallTypeLayerInfos wallTypeLayerInfos, int index, bool isLeftTreeView)
		{
			TreeNode treeNode = new TreeNode(wallTypeLayerInfos.TypeName);
			treeNode.Tag = index;
			if (isLeftTreeView)
			{
				this.treeViewUserSelect.Nodes.Add(treeNode);
			}
			else
			{
				this.treeViewUserNew.Nodes.Add(treeNode);
			}
			for (int i = 0; i < wallTypeLayerInfos.LayerList.Count; i++)
			{
				TreeNode treeNode2 = new TreeNode(wallTypeLayerInfos.LayerList[i].MaterialName);
				if (isLeftTreeView && i >= wallTypeLayerInfos.FirstCoreLayerIndex && i <= wallTypeLayerInfos.LastCoreLayerIndex)
				{
					treeNode2.BackColor = System.Drawing.Color.LightGray;
				}
				treeNode.Nodes.Add(treeNode2);
			}
			treeNode.Expand();
		}

		private void rdbNotSplit_Click(object sender, EventArgs e)
		{
			if (this.rdbNotSplit.Checked)
			{
				this.OperationTypeChanged();
			}
		}

		private void rdbSplitStyle1_Click(object sender, EventArgs e)
		{
			if (this.rdbSplitStyle1.Checked)
			{
				this.OperationTypeChanged();
			}
		}

		private void rdbSplitStyle2_Click(object sender, EventArgs e)
		{
			if (this.rdbSplitStyle2.Checked)
			{
				this.OperationTypeChanged();
			}
		}

		private void rdbCustom_Click(object sender, EventArgs e)
		{
			if (this.rdbCustom.Checked)
			{
				this.OperationTypeChanged();
			}
		}

		private void OperationTypeChanged()
		{
			int index = -1;
			if (!this.IsSelectedWallTypeNode(ref index))
			{
				return;
			}
			if (this.rdbNotSplit.Checked)
			{
				this.m_OperationTypeByType[index] = WallTypeSplitForm.OperationType.OT_NotSplit;
			}
			else if (this.rdbSplitStyle1.Checked)
			{
				this.m_OperationTypeByType[index] = WallTypeSplitForm.OperationType.OT_WholeSurfaceLayer;
			}
			else
			{
				if (!this.rdbSplitStyle2.Checked)
				{
					this.btnCombination.Enabled = true;
					this.btnDecomposition.Enabled = true;
					this.treeViewUserNew.CheckBoxes = true;
					this.treeViewUserNew.DrawMode = TreeViewDrawMode.OwnerDrawAll;
					this.m_OperationTypeByType[index] = WallTypeSplitForm.OperationType.OT_Custom;
					this.UpdateUserNewTreeView(true);
					return;
				}
				this.m_OperationTypeByType[index] = WallTypeSplitForm.OperationType.OT_MultiSurfaceLayer;
			}
			this.btnCombination.Enabled = false;
			this.btnDecomposition.Enabled = false;
			this.treeViewUserNew.CheckBoxes = false;
			this.treeViewUserNew.DrawMode = TreeViewDrawMode.Normal;
			this.UpdateUserNewTreeView(true);
		}

		private void treeViewUserNew_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			if (this.CurrentOperationType() != WallTypeSplitForm.OperationType.OT_Custom)
			{
				return;
			}
			if (this.treeViewUserNew.Nodes.Count == 0)
			{
				return;
			}
			if (e.Node != null && e.Node.Parent != null)
			{
				TreeViewCheckBoxHideController.HideCheckBox(e.Node);
			}
			e.DrawDefault = true;
		}

		private void btnCombination_Click(object sender, EventArgs e)
		{
			int index = -1;
			if (!this.IsSelectedWallTypeNode(ref index))
			{
				return;
			}
			List<int> list = new List<int>();
			if (!this.GetContinuousCheckedNodesIndex(ref list))
			{
				YJKMessageBox.Show("合并项不连续，无法合并");
				return;
			}
			List<WallLayerInfos> list2 = new List<WallLayerInfos>();
			for (int i = list.Count - 1; i >= 0; i--)
			{
				int index2 = list[i];
				list2.InsertRange(0, this.m_UserNewTypeList[index][index2].LayerList);
				this.m_UserNewTypeList[index].RemoveAt(index2);
			}
			WallTypeLayerInfos item = this.CreateWallTypeLayerInfosByWallLayerInfosList(list2);
			this.m_UserNewTypeList[index].Insert(list[0], item);
			this.UpdateUserNewTreeView(false);
		}

		private void btnDecomposition_Click(object sender, EventArgs e)
		{
			int index = -1;
			if (!this.IsSelectedWallTypeNode(ref index))
			{
				return;
			}
			List<int> checkedNodesIndex = this.GetCheckedNodesIndex();
			for (int i = checkedNodesIndex.Count - 1; i >= 0; i--)
			{
				int num = checkedNodesIndex[i];
				List<WallLayerInfos> layerList = this.m_UserNewTypeList[index][num].LayerList;
				if (layerList.Count != 1)
				{
					this.m_UserNewTypeList[index].RemoveAt(num);
					int num2 = 0;
					foreach (WallLayerInfos layerInfos in layerList)
					{
						WallTypeLayerInfos item = this.CreateWallTypeLayerInfosByWallLayerInfos(layerInfos);
						this.m_UserNewTypeList[index].Insert(num + num2, item);
						num2++;
					}
				}
			}
			this.UpdateUserNewTreeView(false);
		}

		private WallTypeLayerInfos CreateWallTypeLayerInfosByWallLayerInfos(WallLayerInfos layerInfos)
		{
			List<WallLayerInfos> list = new List<WallLayerInfos>();
			list.Add(layerInfos);
			return new WallTypeLayerInfos(("HY-" + layerInfos.MaterialName + "-" + layerInfos.Thickness).TrimEnd(Array.Empty<char>()), 0, 0, list);
		}

		private WallTypeLayerInfos CreateWallTypeLayerInfosByWallLayerInfosList(List<WallLayerInfos> layerInfosList)
		{
			string text = "";
			string text2 = "";
			foreach (WallLayerInfos wallLayerInfos in layerInfosList)
			{
				text = text + wallLayerInfos.MaterialName + " ";
				text2 = text2 + wallLayerInfos.Thickness + " ";
			}
			return new WallTypeLayerInfos(("HY-" + text + text2).TrimEnd(Array.Empty<char>()), 0, layerInfosList.Count - 1, layerInfosList);
		}

		private List<int> GetCheckedNodesIndex()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.treeViewUserNew.Nodes.Count; i++)
			{
				if (this.treeViewUserNew.Nodes[i].Checked)
				{
					list.Add(i);
				}
			}
			return list;
		}

		private bool GetContinuousCheckedNodesIndex(ref List<int> continuousChecedNodesIndex)
		{
			List<TreeNode> list = new List<TreeNode>();
			List<int> list2 = new List<int>();
			for (int i = 0; i < this.treeViewUserNew.Nodes.Count; i++)
			{
				TreeNode treeNode = this.treeViewUserNew.Nodes[i];
				if (treeNode.Checked)
				{
					list.Add(treeNode);
					list2.Add(i);
				}
			}
			if (list.Count < 2)
			{
				return false;
			}
			for (int i = 0; i < list2.Count - 1; i++)
			{
				if (list2[i + 1] - list2[i] != 1)
				{
					return false;
				}
			}
			continuousChecedNodesIndex.AddRange(list2);
			return true;
		}

		private Document m_Doc;

		private List<WallTypeLayerInfos> m_UserSelectTypeList = new List<WallTypeLayerInfos>();

		private List<WallTypeSplitForm.OperationType> m_OperationTypeByType = new List<WallTypeSplitForm.OperationType>();

		private List<List<WallTypeLayerInfos>> m_UserNewTypeList = new List<List<WallTypeLayerInfos>>();

		private enum OperationType
		{
			OT_NotSplit = -1,
			OT_WholeSurfaceLayer,
			OT_MultiSurfaceLayer,
			OT_Custom
		}
	}
}
